import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd/message';
import { DatePipe } from '@angular/common';
import { CommonService } from '../../../services-common/common.service';
import { CustomerIdolRechargeService } from './customer-idol-recharge.service';

@Component({
  selector: 'app-customer-idol-recharge',
  templateUrl: './customer-idol-recharge.component.html',
  styleUrls: ['./customer-idol-recharge.component.scss'],
  providers: [DatePipe]
})
export class CustomerIdolRechargeComponent implements OnInit {

  seachParams: any = {
    num: '', // 综合搜索
    beginTime: '',
    endTime: '',
    auditStatus: ''
  };
  dateArrs: any = [];

  tableParams: any = {
    tableLoading: false,
    page: 1,
    pageSize: 20,
    total: 0
  };
  tableData: any = [];

  // 充值 Modal
  isVisible: boolean = false;
  validateForm!: FormGroup;

  // 审核
  isExamineVisible: boolean = false;
  examineItem: any = {};

  // 批量发放爱豆
  grantIdolLoading = false;

  constructor(
    private fb: FormBuilder,
    private message: NzMessageService,
    private datePipe: DatePipe,
    private commonService: CommonService,
    private services: CustomerIdolRechargeService
  ) { }

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      userId: ['', [Validators.required]],
      number: ['', [Validators.required]],
      title: ['', [Validators.required]],
      remarks: [''],
    });

    this.loadTable();
  }

  async loadTable() {
    let params = {
      ...this.seachParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.tableLoading = true;
    const res: any = await this.services.queryIdolList(params);
    this.tableParams.tableLoading = false;
    if (res.code == 0) {
      const { records, total } = res.data || {};
      this.tableData = records || [];
      this.tableParams.total = total || 0;
    } else {
      this.message.error(res.message);
    }
  }

  seachData() {
    this.tableParams.page = 1;
    this.loadTable();
  }
  resetData() {
    for (let key in this.seachParams) {
      this.seachParams[key] = '';
    }
    this.dateArrs = [];
    this.tableParams.page = 1;
    this.loadTable();
  }

  timeChange(result: any) {
    this.seachParams.beginTime = result.length ? this.datePipe.transform(result[0], 'yyyy-MM-dd') : '';
    this.seachParams.endTime = result.length ? this.datePipe.transform(result[1], 'yyyy-MM-dd') : '';
  }

  // 充值
  showModal() {
    this.validateForm.reset();
    this.isVisible = true;
  }
  handleCancel() {
    this.isVisible = false;
  }
  async submitForm() {
    if (this.validateForm.valid) {
      const res: any = await this.services.saveIdolData({...this.validateForm.value});
      if (res.code == 0) {
        this.message.success('操作成功');
        this.isVisible = false;
        this.loadTable();
      } else {
        this.message.error(res.message);
      }
    } else {
      Object.values(this.validateForm.controls).forEach(control => {
        if (control.invalid) {
          control.markAsDirty();
          control.updateValueAndValidity({ onlySelf: true });
        }
      });
    }
  }

  // 审核
  showExamine(item: any) {
    this.examineItem = item;
    this.isExamineVisible = true;
  }
  async submitExamine(num: number) {
    let params = {
      id : this.examineItem.id,
      auditStatus: num
    }
    const res: any = await this.services.auditIdolData(params);
    if (res.code == 0) {
      this.message.success('操作成功');
      this.isExamineVisible = false;
      this.loadTable();
    } else {
      this.message.error(res.message);
    }
  }

  // 当前页码改变时的回调函数
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  // 页数改变时的回调函数
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  // 审核状态 0 待审核 1.审核通过 2.审核失败
  auditStatusFilter(type: number) {
    switch (type) {
      case 0:
        return '待审核';
      case 1:
        return '审核通过';
      case 2:
        return '审核失败';
      default:
        return '-';
    }
  }

  // 发放爱豆
  uploadCustomRequest = (options: any): any => {
    const {file} = options;
    this.uploadFileInterface(file);
  }

  async uploadFileInterface(file: File) {
    if (this.grantIdolLoading || this.commonService.isEmpty(file)) {
      return
    }

    const formData: FormData = new FormData();
    formData.append('file', file);

    this.grantIdolLoading = true;
    const {code, data, message}: any = await this.services.batchGrantIdolData(formData);
    this.grantIdolLoading = false;

    if (code === 0) {
      this.message.success('操作成功');
      this.tableParams.page = 1;
      this.loadTable();
    } else {
      this.message.error(message);
    }
  }
}
